<template>
    <h2>增加房型</h2>
    <input type="button" value="房型展示" @click="showkskma">
    <input type="button" value="录入房号" @click="howmns">
    <table>
        <tr>
            <td>房型名称</td>
            <td>
                <input type="text" v-model="prist.name">
            </td>
        </tr>
        <tr>
            <td>房型图片</td>
            <td>
                <input type="file" @change="imhaujwms">
                <img :src="src" width="60" height="60" v-if="prist.picture">
            </td>
        </tr>
        <tr>
            <td>房型大小</td>
            <td>
                <input type="text" v-model="prist.squee">
            </td>
        </tr>
        <tr>
            <td>入住人数</td>
            <td>
                <input type="text" v-model="prist.maxnum">
            </td>
        </tr>
        <tr>
            <td>描述</td>
            <td>
                <textarea cols="30" rows="3" v-model="prist.remark"></textarea>
            </td>
        </tr>
        <tr>
            <td></td>
            <td>
                <input type="button" value="添加" @click="owiksbje">
            </td>
        </tr>
    </table>
  </template>
  
  <script setup lang="ts">
  import {reactive, toRefs} from 'vue'
  import {useRouter,useRoute} from 'vue-router'
  import axios from 'axios'
  const router=useRouter()
  const route=useRoute()

  let data=reactive({
    prist:{
        name:'',
        picture:'',
        squee:'',
        maxnum:'',
        remark:''
    },
    src:''
  })
  let {prist,src} =toRefs(data)

  const imhaujwms=(e:any)=>{
    var form=new FormData()
    var obj=e.target.files[0]
    form.append('file',obj)
    axios({
        url:'/api/Upfile/Upload',
        method:'post',
        data:form
    })
    .then(res=>{
        if(res.data.code==11){
            alert(res.data.ms)
            return;
        }
        if(res.data.code==12){
            alert(res.data.ms)
            return;
        }else{
            src.value='https://localhost:7214'+res.data.filename;
            prist.value.picture=res.data.filename;
        }
    })
  }

  const owiksbje=()=>{
    var obj={
        "rname": prist.value.name,
  "picture": prist.value.picture,
  "squre": prist.value.squee,
  "peopnum": prist.value.maxnum,
  "remark": prist.value.remark,
  "isDeleted": false
    }
    axios({
        url:'/api/Room/Addtype',
        method:'post',
        data:obj
    })
    .then(res=>{
        if(res.data.data==1){
            alert("添加房型成功")
            router.push('showtype')
        }else if(res.data.data==-2){
            alert("房型名称不能重复")
        }else{
            alert("添加房型失败")
        }
    })
  }

  const showkskma=()=>{
    router.push('showtype')
  }

  const howmns=()=>{
    router.push('addnum')
  }
  </script>
  
  <style scoped>
  
  </style>